package com.whales.shared.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import anyframe.common.Page;
import anyframe.common.util.SearchVO;
import anyframe.core.query.AbstractDAO;
import anyframe.core.query.QueryServiceException;

public class DaoImpl extends AbstractDAO {
	/**
	 * VO 또는 Domain객체를 INSERT한다.
	 * 
	 * @param queryId
	 *            Query Mapping File에 정의된 Query ID
	 * @param insertObject
	 *            DB에 추가할 VO또는 Domain Object
	 * @return int DB에 INSERT된 Record의 수
	 * @throws QueryServiceException
	 */
	public int create(String queryId, Object insertObject)
			throws QueryServiceException {
		return getQueryService().create(queryId,
				new Object[] { new Object[] { "vo", insertObject } });
	}
	
	@SuppressWarnings("rawtypes")
	public int create(String queryId, List insertList)
			throws QueryServiceException {

		List<Object> batchUpdteList = new ArrayList<Object>();
		for (Object insertObj : insertList) {
			Object[] object = new Object[] { new Object[] { "vo", insertObj } };
			batchUpdteList.add(object);
		}
		
		getQueryService().batchUpdate(queryId, batchUpdteList);
		return 0;
	}

	/**
	 * VO 또는 Domain객체를 UPDATE한다.
	 * 
	 * @param queryId
	 *            Query Mapping File에 정의된 Query ID
	 * @param updateObject
	 *            DB에 수정할 VO또는 Domain Object
	 * @return int DB에 UPDATE된 Record의 수
	 * @throws QueryServiceException
	 */
	public int update(String queryId, Object updateObject)
			throws QueryServiceException {
		return getQueryService().update(queryId,
				new Object[] { new Object[] { "vo", updateObject } });
	}

	@SuppressWarnings("rawtypes")
	public int update(String queryId, List updateList)
			throws QueryServiceException {

		List<Object> batchUpdteList = new ArrayList<Object>();
		for (Object updateObj : updateList) {
			Object[] object = new Object[] { new Object[] { "vo", updateObj } };
			batchUpdteList.add(object);
		}
		
		getQueryService().batchUpdate(queryId, batchUpdteList);
//		return getQueryService().batchUpdate(queryId, batchUpdteList);
		return 0;
	}

	/**
	 * VO 또는 Domain객체를 DELETE한다.
	 * 
	 * @param queryId
	 *            Query Mapping File에 정의된 Query ID
	 * @param deleteObject
	 *            DB에 수정할 VO또는 Domain Object
	 * @return int DB에 DELETE된 Record의 수
	 * @throws QueryServiceException
	 */
	public int delete(String queryId, Object deleteObject)
			throws QueryServiceException {
		return getQueryService().remove(queryId,
				new Object[] { new Object[] { "vo", deleteObject } });
	}

	public int delete(String queryId, Object[] deleteObject)
			throws QueryServiceException {
		return getQueryService().remove(queryId, deleteObject);
	}

	public int[] delete(String queryId, List<?> deleteList)
			throws QueryServiceException {
		List<Object> batchUpdteList = new ArrayList<Object>();
		for (Object deleteObj : deleteList) {
			Object[] object = new Object[] { new Object[] { "vo", deleteObj } };
			batchUpdteList.add(object);
		}
		return getQueryService().batchUpdate(queryId, deleteList);
	}

	/**
	 * List 조회
	 * 
	 * @param queryId
	 * @param tableName
	 * @param searchVO
	 * @return
	 * @throws QueryServiceException
	 */
	@SuppressWarnings("rawtypes")
	public List findList(String queryId, Object searchVO)
			throws QueryServiceException {
		return (List) getQueryService().find(queryId,
				new Object[] { new Object[] { "vo", searchVO } });
	}

	@SuppressWarnings("rawtypes")
	public List findList(String queryId, Object[] searchVO)
			throws QueryServiceException {
		return (List) getQueryService().find(queryId, searchVO);
	}

	@SuppressWarnings("rawtypes")
	public List findList(String queryId, Map map) throws QueryServiceException {
		return (List) getQueryService().find(queryId,
				new Object[] { new Object[] { "vo", map } });
	}

	/**
	 * 단건 조회
	 * @param queryId
	 * @param searchVO
	 * @return
	 * @throws QueryServiceException
	 */
	@SuppressWarnings("unchecked")
	public Object findInfo(String queryId, Object searchVO)
			throws QueryServiceException {
		Iterator<Map<String, Object>> rsqueryItr = getQueryService().find(
				queryId, new Object[] { new Object[] { "vo", searchVO } })
				.iterator();
		while (rsqueryItr.hasNext()) {
			return rsqueryItr.next();
		}
		return null;
	}

	/**
	 * Paging 조회
	 * 
	 * @param queryId
	 * @param searchVO
	 * @return
	 * @throws QueryServiceException
	 */
	public Page findPagingList(String queryId, SearchVO searchVO)
			throws QueryServiceException {
		return findPagingList(queryId, searchVO, searchVO.getPageIndex());
	}

	public Page findPagingList(String queryId, Object searchVO, int pageIndex)
			throws QueryServiceException {
		return findPagingList(queryId, searchVO, pageIndex, 10);
	}

	public Page findPagingList(String queryId, Object searchVO, int pageIndex,
			int pageSize) throws QueryServiceException {
		return findPagingList(queryId, searchVO, pageIndex, pageSize, 10);
	}

	public Page findPagingListByRowIndex(String queryId, Object searchVO,
			int start, int limit) throws QueryServiceException {
		return findPagingListByRowIndex(queryId, searchVO, start, limit, 10);
	}

	public Page findPagingListByRowIndex(String queryId, Object searchVO,
			int start, int limit, int pageUnit) throws QueryServiceException {
		return findPagingList(queryId, searchVO,
				Math.round((start + 1) / limit) + 1, limit, pageUnit);
	}

	public Page findPagingList(String queryId, Object searchVO, int pageIndex,
			int pageSize, int pageUnit) throws QueryServiceException {
		super.setFindListPostfix("");
		super.setFindPrefix("");
		return findListWithPaging(queryId, searchVO, pageIndex, pageSize,
				pageUnit);
	}
}
